package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.HelpCallBean;
import com.mycjj.android.bean.HelpCallBean.HelpCall;
import com.mycjj.android.bean.HelpCallBean.HelpCall.CallDetail;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class QiuZhuActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_qiuzhu);
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("求助电话");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				QiuZhuActivity.this.finish();
			}
		});
	}

	private ListView lv_qiuzhu;
	private QzAdapter qzAdapter;
	private String token;

	private void init(){
		token = Preferences.getSharedPreferences(this, "user_token", "");
		initHeader();
		lv_qiuzhu = (ListView)findViewById(R.id.lv_qiuzhu);
		qzAdapter = new QzAdapter(this);
		lv_qiuzhu.setAdapter(qzAdapter);
		lv_qiuzhu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(callDetailList == null || callDetailList.size() == 0){
					Log.e("mark", "callDetailList is empty!!");
					return;
				}
				CallDetail detail = callDetailList.get(position);
				Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + detail.getTel()));
			     //启动
				startActivity(phoneIntent);
			}
		});
	}

	private List<CallDetail> callDetailList;

	private void initData() {
		String store_id = Preferences.getSharedPreferences(this, "storeId", "");
		Log.e("mark","store_id:"+store_id);
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("page", "0");
		params.put("store_id", store_id);
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", token);
		request.requestGetHelpCallsWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						HelpCallBean bean =  ProjectDataManage.getInstance().getHelpCallBean();
						if (bean.getCode() == 200) {
							HelpCall helpCall = bean.getData();
							if (helpCall != null) {
								callDetailList = helpCall.getList();
								qzAdapter.setList(callDetailList);
							}
						}
					}
				});
	}

	public class QzAdapter extends TAdapter<CallDetail> {

		public QzAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_qiuzhu_view, null);
				holder = new ViewHolder();
				holder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_qz_title);
				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_qz_pnumber);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			CallDetail detail = mList.get(position);
			holder.tv_title.setText(detail.getTitle());
			holder.tv_number.setText(String.valueOf(detail.getTel()));
			return convertView;
		}

		class ViewHolder {
			TextView tv_title;
			TextView tv_number;
		}

	}

}
